﻿using AutoMapper;
using BaseData.API.Write.Applications.MCI.Commands;
using BaseData.Domain.MCI;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;
using NPOI.SS.Formula.Functions;
using System.Linq.Expressions;

namespace BaseData.API.Write.Applications.MCI.CommandHandlers
{
    /// <summary>
    /// 编辑就诊人信息-命令
    /// </summary>
    public class UpdPatientCommandHandler : IRequestHandler<UpdPatientCommand, ApiResult<int>>
    {
        /// <summary>
        /// 就诊人仓储
        /// </summary>
        private readonly IBaseRepository<Patient> patientRep;
        private readonly IMapper mapper;

        public UpdPatientCommandHandler(IBaseRepository<Patient> PatientRep,IMapper mapper)
        {
            patientRep = PatientRep;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdPatientCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                

                var entity = mapper.Map<Patient>(request);
                var res= await patientRep.UpdateAsync(entity);
                if(res>0)
                {
                    result.Code = ApiEnum.修改成功;
                    result.Message = ApiEnum.修改成功.ToString();
                    result.Data = res;
                }
                else
                {
                    result.Code = ApiEnum.修改失败;
                    result.Message = ApiEnum.修改失败.ToString();
                    result.Data = res;
                }
                return await Task.FromResult(result);
                
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
